package aqua.smile.springboot.srvapi.controller;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@RestController
@RequestMapping("/api")
@Slf4j
public class ApiController {

    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Integer id) {
        log.info("get run..");
        return Result.okRs(
                MapBuilder.create()
                        .put("id", id)
                        .put("name", IdUtil.fastSimpleUUID())
                        .build()
        );
    }

    @PostMapping("/post")
    public Result post(@RequestBody UserEntity userEntity) {
        log.info("post run..");
        userEntity.setId(System.currentTimeMillis());
        return Result.okRs(userEntity);
    }

    @PutMapping("/put")
    public Result put(@RequestBody UserEntity userEntity) {
        log.info("put run..");
        return Result.okRs(userEntity);
    }

    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id) {
        log.info("del run..");
        return Result.okRs("删除成功");
    }

    @PostMapping("/post2")
    public Result post2(@RequestBody UserEntity userEntity, HttpServletRequest request, HttpServletResponse response) {
        log.info("post2 run..");

        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String header = request.getHeader(name);
            log.info("header --- {} : {}", name, header);
        }

        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            String val = request.getParameter(name);
            log.info("params --- {} : {}", name, val);
        }

        userEntity.setId(System.currentTimeMillis());
        return Result.okRs(userEntity);
    }


}
